package com.siruier.boss.ui.activity.user

import android.view.View
import androidx.activity.result.ActivityResult
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import com.siruier.boss.api.core.MD5Util
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.loginApi
import com.siruier.boss.common.formatPrice
import com.siruier.boss.common.formatPriceRmb
import com.siruier.boss.databinding.ActivityWithdrawalBinding
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.cache.UserCache
import com.siruier.boss.ui.ext.toastMessageLong
import com.siruier.boss.ui.ext.value
import com.siruier.boss.ui.helper.AppConfigHelper.loadConfig
import com.siruier.boss.ui.helper.InjectBundle
import com.siruier.boss.ui.helper.checkPayPasswordAndRealNameAuth
import com.siruier.boss.ui.utils.Utils
import com.siruier.boss.ui.utils.variableView
import java.math.BigDecimal

class WithdrawalActivity : BaseActivity<ActivityWithdrawalBinding>() {
    companion object {
        private val WITHDRAWA_MIN = BigDecimal("100")
    }

    private val busiType: Int by InjectBundle { 1 }
    private var allMoney: BigDecimal = BigDecimal.ZERO

    override fun initView() {
        Utils.invasionStatusBar(this)
        Utils.setDefaultStatusBarFont(this, false)
        bindViewClick(vb.textAll, vb.buttonSettingPayPas, vb.buttonSubmit)

        variableView {
            addTextView(vb.etMoney)
            addTextView(vb.etPayPassword)
            addOnValidationResultListener(vb.buttonSubmit)
        }
        settingPayPas()
        loadConfig {
            vb.tips.text = when (busiType) {
                1 -> it.appWithdrawDescYj
                3 -> it.appWithdrawDescGm
                else -> ""
            }
        }
    }

    override fun initData() {
        UserCache.getUserInfoBean().apply {
            vb.tvBankNo.text = cardNo
            vb.tvName.text = cardOwner
        }
        UserCache.getBalanceTotal().apply {
            when (busiType) {
                1 -> {
                    allMoney = integral
                    vb.tvMoney.text = integral.formatPriceRmb()
                    vb.tvTotalMoney.text = "总金额：${totalIntegral.formatPrice()}"
                    vb.tvCashMoney.text = "已提现：${withdrawalAmount.formatPrice()}"
                    vb.tvFrozenMoney.text = "冻结金额：${frozenWithdrawIntegral.formatPrice()}"
                }
                3 -> {
                    allMoney = balance
                    vb.tvMoney.text = "￥${balance.formatPrice()}"
                    vb.tvTotalMoney.text = "总金额：${totalBalance.formatPrice()}"
                    vb.tvCashMoney.text = "已提现：${withdrawalBalance.formatPrice()}"
                }
            }
        }
    }

    private fun settingPayPas(result: ActivityResult? = null) {
        vb.buttonSettingPayPas.isVisible = UserCache.getUserInfoBean().setPayPassword != 1
        vb.etPayPassword.isInvisible = vb.buttonSettingPayPas.isVisible
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.textAll -> vb.etMoney.setText(allMoney.formatPrice())
            vb.buttonSettingPayPas -> launchStartActivityForResult(
                RealNameAuthActivity::class.java,
                activityForResult = ::settingPayPas
            )
            vb.buttonSubmit -> {
                val money = BigDecimal(vb.etMoney.text.toString())
                if (money < WITHDRAWA_MIN) {
                    "提现金额必须大于100".toastMessageLong()
                    return
                }
                launchUI({
                    checkPayPasswordAndRealNameAuth()
                    loginApi.withdraw(
                        money.toString(),
                        busiType,
                        MD5Util.md5(vb.etPayPassword.value)
                    ).toData()
                    UserCache.saveBalanceTotal(loginApi.balanceTotal().toData())
                }, ApiObserver(this) {
                    "提现申请成功".toastMessageLong()
                    finish()
                })
            }
        }
    }
}